<?php

namespace App\Util;

use Elite\Infrastructure\Server\FdHolder;
use Elite\Infrastructure\Server\Message;
use Swoole\WebSocket\Frame;

class MessageFormatUtil {

    public static function convertToMessage(Frame $frame, FdHolder $fdHolder) : Message {
        $data = json_decode($frame->data, true);
        $from = $fdHolder->get($frame->fd);
        $toFd = null;
        if (isset($data['to'])) {
            $toFd = $fdHolder->get($data['to']);
        }
        return new Message($data['cmd'] ?? "", $from, $frame->fd, $data['to'] ?? null, $toFd, $data['msg'] ?? null);
    }


}